import { Injector , Type } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { LocalizationService } from '@core/net/localization/localization.service';
import { PermissionCheckerService } from '@core/net/auth/permission-checker.service';
import { FeatureCheckerService } from '@core/net/features/feature-checker.service';

import { SettingService } from '@core/net/settings/setting.service';
import { MessageService } from '@core/net/message/MessageService';
import { AbpMultiTenancyService } from '@core/net/multi-tenancy/abp-multi-tenancy.service';
import { AppSessionService } from '@shared/common/session/app-session.service';
import { AppUiCustomizationService } from '@shared/common/ui/app-ui-customization.service';

import { Abp } from '@core/net/abp';

import { NotificationService } from './notificationService';

import {NzModalService, NzMessageService} from 'ng-zorro-antd';


export abstract class AppComponentBase {

    localizationSourceName = AppConsts.localization.defaultLocalizationSourceName;

    localization: LocalizationService;
    permission: PermissionCheckerService;
    feature: FeatureCheckerService;
    notify: NotificationService;
    setting: SettingService;
    message: MessageService;
    multiTenancy: AbpMultiTenancyService;
    appSession: AppSessionService;
    ui: AppUiCustomizationService;

    _modal: NzModalService;

    loading = false;

    constructor(injector: Injector) {
        this.localization = injector.get(LocalizationService);
        this.permission = injector.get(PermissionCheckerService);
        this.feature = injector.get(FeatureCheckerService);
        this.notify = injector.get(NotificationService);
        this.setting = injector.get(SettingService);
        this.message = injector.get(MessageService);
        this.multiTenancy = injector.get(AbpMultiTenancyService);
        this.appSession = injector.get(AppSessionService);
        this.ui = injector.get(AppUiCustomizationService);

        this._modal = injector.get(NzModalService);
    }

    l(key: string, ...args: any[]): string {
        return this.ls(this.localizationSourceName, key, args);
    }

    ls(sourcename: string, key: string, ...args: any[]): string {
        let localizedText = this.localization.localize(key, sourcename);

        if (!localizedText) {
            localizedText = key;
        }

        if (!args || !args.length) {
            return localizedText;
        }

        args[0].unshift(localizedText);

        return Abp.utils.formatString.apply(this, args[0]);
    }

    isGranted(permissionName: string): boolean {
        return this.permission.isGranted(permissionName);
    }

    isGrantedAny(...permissions: string[]): boolean {
        if (!permissions) {
            return false;
        }

        for (const permission of permissions) {
            if (this.isGranted(permission)) {
                return true;
            }
        }

        return false;
    }

    s(key: string): string {
        return Abp.setting.get(key);
    }


    showModal(content: string | Type<any>,
        componentParams?: object,
        onOkCallbakc?: () => void ,
    ) {

            const options = {
                content: content,
                wrapClassName: 'modal-lg',
                footer: false,
                componentParams: componentParams
            };
            this
                ._modal
                .open(options)
                .subscribe(result => {

                    if (result === 'onOk') {
                        onOkCallbakc();
                    }
                });


    }


}
